<?php
namespace app\admin\controller;
use x\my\Umeng;
class User extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!='') $map[] = ['and','a.name|a.phone','like',"%{$d['keyword']}%"];
            if($d['level']!='') $map[] = ['and','a.level','=',$d['level']];
            if($d['status']!='') $map[] = ['and','a.status','=',$d['status']];
            if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
            $w  = where($map);
            $rs = $this->db->query("select a.*,b.name s_name from user a left join user b on a.s_id = b.id $w[0] order by id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'status' => function($v){return c("type.status.{$v}");},
                    'level' => function($v){return c("type.user_level.{$v}");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }
    //订单列表
    public function order_lists(){
        if(is_post()){
            $d = $this->post;
            $sql = "
                SELECT
                    c.title,
                    c.phone,
                    w.name,
                    w.phone AS w_phone,
                    g.title AS g_title,
                    g.money,
                    og.id,
                    og.order_no,
                    og.cost_price,
                    og.price,
                    og.pv,
                    og.start_time,
                    og.end_time,
                    og.fw_times,
                    og.fw_address,
                    og.fw_name,
                    og.fw_phone,
                    og.note,
                    og.fw_note,
                    og.status,
                    og.create_time
                FROM
                    order_goods AS og
                LEFT JOIN
                    company AS c
                ON
                    og.company_id = c.id
                LEFT JOIN
                    worker AS w
                ON
                    og.worker_id = w.id
                LEFT JOIN
                    goods AS g
                ON
                    og.goods_id = g.id
                WHERE
                    og.user_id = ?
            ";
            $rs = $this->db->query($sql.limit($d['page'],$d['limit']), [ $d['id'] ] ,'',true);
            if($rs){
                $rule = [
                    'status' => function($v){return c("type.order_status.{$v}");},
                    'create_time' => 'com_time_format',
                    'start_time' => 'com_time_format',
                    'end_time' => 'com_time_format',
                    'fw_times' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
    }
    //添加用户
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'name' => "require",
                'phone'=> "mobile"
            ];
            $msg = [
            ];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            //推荐人
            if($d['s_id']){
              $rs = $this->db->query('select s_tree from user where id=?',[$d['s_id']],1);
              $d['s_tree'] = $rs['s_tree'].$d['s_id'].',';
              $d['s_level'] = count(explode(',',$d['s_tree']))-2;
            }else{
              $d['s_tree'] = ",0,";
              $d['s_level'] = 0;
            }
            $d['username'] = $d['name'];
            $id = $this->db->insert('user',$d);
            if ($id) {
                m('log','xcom')->record($this->userinfo['id'], "添加用户数据:".json_encode($d,JSON_UNESCAPED_UNICODE),"admin_user");
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        return $this->fetch();
    }
    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'phone'=>'mobile',
            ];
            $msg = [
            ];
            check($rule,$msg,$d);
            //推荐人
            if($d['s_id']){
              if($d['s_id']==$d['id']){
                err('不能设置自己为自己的推荐人');
              }
              $rs = $this->db->query('select s_tree from user where id=?',[$d['s_id']],1);
              $d['s_tree'] = $rs['s_tree'].$d['s_id'].',';
              $d['s_level'] = count(explode(',',$d['s_tree']))-2;
            }else{
              $d['s_tree'] = ",0,";
              $d['s_level'] = 0;
            }
            $d['username'] = $d['name'];
            $this->db->update("user",$d);
            m('log','xcom')->record($this->userinfo['id'], "编辑用户数据:".json_encode($d,JSON_UNESCAPED_UNICODE),"admin_user");
            cookie('js_msg','修改成功');
            cookie('js_function','preload');
            return suc();
        }
        $id     = $this->get["id"];
        $info   = $this->db->find("user",$id);
        $s_rs = $this->db->find("user",$info['s_id']);
        $s_info = "";
        if($s_rs) $s_info = "姓名:{$s_rs['name']} 电话:{$s_rs['phone']} 等级:".c('type.user_level')[$s_rs['level']];
        $this->fetch(["info" => $info,'s_info'=>$s_info]);
    }

    //选择会员
    function cuser(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!='') $map[] = ['and','a.name|a.phone','like',"%{$d['keyword']}%"];
            if($d['level']!='') $map[] = ['and','a.level','=',$d['level']];
            $w  = where($map);
            $rs = $this->db->query("select a.*,b.name s_name from user a left join user b on a.s_id = b.id $w[0] order by id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'status' => function($v){return c("type.status.{$v}");},
                    'level' => function($v){return c("type.user_level.{$v}");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }

    //详情
    public function detail(){
        $id = $this->get("id");
        $map = [];
        $map[] = ['and','s_id','=',$id];
        $w  = where($map);
        $s_user = array();
        $info = $this->db->query("select * from user where id = ?",[$id],1);
        $user = $this->db->query("select * from user where s_id = ?",[$id]);
        $s_tree = array_filter( explode(',',$info['s_tree']) );
        foreach ($s_tree as $k => $v) {
            $s_user[] = $this->db->query("select * from user where id = ?",[$v],1);
        }
        $bill = $this->db->query("SELECT * FROM user_bill WHERE user_id = ?" , [$id]);

        $this->fetch(['info'=>$info,'user'=>$user,'s_user'=>$s_user,'bill'=>$bill]);
    }

    //变动资金
    public function editMoney(){
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'id'         =>  'require',
                'money_type' => 'require',
                'money'      =>  'require|float',
                'note'       =>  'require',
            ];
            $msg = [
                'money_type.require' =>'币种不能为空',
                'money.require' =>'金额不能为空',
                'money.float'   =>'金额格式不对',
                'note.require'  =>'请填写备注信息',
            ];
            check($rule,$msg,$d);
            $rs = s('OrderUmoney','c')->doit($d['id'], $this->userinfo['id'], $d['money_type'], $d['money'], $d['note']);
            cookie('js_function','preload');
            if($rs){
                cookie('js_msg','变动资金成功');
                suc();
            }else{
                err(s('OrderUmoney','c')->getError());
            }
        }
        $id=$this->get('id');
        //模板赋值
        $info = $this->db->find('user',$id);
        $this->fetch(['info'=>$info]);
    }

    //升级
    public function upLevel(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'    =>  'require',
                'level' => 'require',
//                'money' => 'require'
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d["money"] = str_replace([1,2,3],[980,1980,19800],$d["level"]);
            $rs = s("UserUpLevel","c")->upLevel($d["id"],$d["level"],$d["money"]);
            if($rs){
                cookie('js_msg','升级成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err(s("UserUpLevel","c")->getError());
            }
        }
        $id=$this->get('id');
        //模板赋值
        $info = $this->db->find('user',$id);
        $this->fetch(['info'=>$info]);
    }
}
